package com.yq.homework.day30;


import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


/**
 * @author 3307235355@qq.com
 * @description
 * @since 2022/06/30 19:44
 * 创建一个任务，它将睡眠随机1-10s，
 * 然后显示睡眠时间并退出。
 * 建并运行一定数量的这个任务 ps:利用线程池
 */
public class Demo02 {
    public static void main(String[] args) {
        // ExecutorService ex1 = Executors.newCachedThreadPool();
        ExecutorService ex1 = Executors.newFixedThreadPool(2);
        ex1.submit(new MyTest());
        ex1.submit(new MyTest());
        ex1.submit(new MyTest());
        ex1.submit(new MyTest());
        ex1.shutdown();

    }
}

class MyTest implements Runnable{
    Random random = new Random();
    @Override
    public void run() {
        try {
            int i = random.nextInt(10);
            // TimeUnit.SECONDS.sleep(i++);
            Thread.sleep((++i)*1000);
            System.out.println(Thread.currentThread().getName()+"睡眠了"+i+"秒！！！");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}